from django.db import models
from django.utils.safestring import mark_safe
from tools.random_str import my_pcb_id


# Create your models here.

class PcbTag(models.Model):
    name = models.CharField(max_length=30, verbose_name='名称')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class PcbCreateCompany(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='名称')
    image = models.ImageField(upload_to="img/pcbbrand/%Y/%m", default='img/pcbbrand/jlc.jpg', blank=True,
                              verbose_name="logo",
                              max_length=100)
    telephone = models.CharField(max_length=20, null=True, blank=True, verbose_name='联系方式')
    address = models.CharField(max_length=50, verbose_name='地址', null=True, blank=True)
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=100, verbose_name='描述', null=True, blank=True)

    class Meta:
        verbose_name = '制板公司'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='160'/>".format(self.image.url))

    show_image.short_description = "公司logo"

    def __str__(self):
        return self.name


class PcbCreateSoftware(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='软件')
    image = models.ImageField(upload_to="img/software/%Y/%m", default='img/software/lc.png', blank=True,
                              verbose_name="logo", max_length=100)
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '绘制软件'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='160'/>".format(self.image.url))

    show_image.short_description = "软件logo"

    def __str__(self):
        return self.name


class PcbCreateSoftwareVersion(models.Model):
    name = models.CharField(max_length=10, verbose_name='版本')
    software = models.ForeignKey(PcbCreateSoftware, on_delete=models.CASCADE, verbose_name='绘制软件')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    def show_software_image(self):
        return self.software.show_image()

    show_software_image.short_description = "软件"

    class Meta:
        verbose_name = '软件版本'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class PcbAuthor(models.Model):
    name = models.CharField(max_length=10, verbose_name='作者')
    image = models.ImageField(upload_to="img/pcbauthor/%Y/%m", default='img/goods/06.jpg', blank=True,
                              verbose_name="头像", max_length=100)
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '作者'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='67'/>".format(self.image.url))

    show_image.short_description = "作者头像"

    def __str__(self):
        return self.name


class Pcb(models.Model):
    my_id = models.CharField(max_length=10, default=my_pcb_id, primary_key=True, verbose_name='电路板编号')
    image = models.ImageField(upload_to='img/pcb/%Y/%m', default='img/goods/06.jpg', blank=True,
                              verbose_name="图片")
    tag = models.ManyToManyField(PcbTag, default='', verbose_name='标签')
    author = models.ForeignKey(PcbAuthor, on_delete=models.CASCADE, verbose_name='作者')
    create_software_version = models.ForeignKey(PcbCreateSoftwareVersion, on_delete=models.CASCADE, verbose_name='软件版本')
    create_company = models.ForeignKey(PcbCreateCompany, on_delete=models.CASCADE, verbose_name='制板公司')
    address = models.URLField(default='', null=True, blank=True, verbose_name='项目地址')
    stock = models.IntegerField(default=5, verbose_name='库存')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=200, verbose_name='描述', null=True, blank=True)
    is_active = models.BooleanField(default=False, verbose_name='合格')
    date_tested = models.DateField(default='', blank=True, null=True, verbose_name='测试时间')

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='67'/>".format(self.image.url))

    def show_software_image(self):
        return self.create_software_version.show_software_image()

    def show_author_image(self):
        return self.author.show_image()

    def show_company_image(self):
        return self.create_company.show_image()

    show_image.short_description = "样板"
    show_software_image.short_description = "绘制软件"
    show_author_image.short_description = "作者"
    show_company_image.short_description = "打样公司"

    class Meta:
        verbose_name = '电路板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.my_id
